package ocean.chinacraft.common.entity.goal.fly;

import net.minecraft.entity.MobEntity;
import net.minecraft.entity.ai.controller.MovementController;
import net.minecraft.entity.ai.goal.Goal;

import java.util.EnumSet;
import java.util.Random;

/**
 * 默认随机飞行
 *
 * @author zhang
 * @since 2022/1/2
 */
public class FlyingRandomGoal<T extends MobEntity> extends Goal {

    private final T entity;

    public FlyingRandomGoal(T entity) {
        this.entity = entity;
        this.setFlags(EnumSet.of(Goal.Flag.MOVE));
    }

    @Override
    public boolean canUse() {
        MovementController movementcontroller = this.entity.getMoveControl();
        if (!movementcontroller.hasWanted()) {
            return true;
        } else {
            double d0 = movementcontroller.getWantedX() - this.entity.getX();
            double d1 = movementcontroller.getWantedY() - this.entity.getY();
            double d2 = movementcontroller.getWantedZ() - this.entity.getZ();
            double d3 = d0 * d0 + d1 * d1 + d2 * d2;
            return d3 < 1.0D || d3 > 3600.0D;
        }
    }

    @Override
    public boolean canContinueToUse() {
        return false;
    }

    @Override
    public void start() {
        Random random = this.entity.getRandom();
        double d0 = this.entity.getX() + (double) ((random.nextFloat() * 2.0F - 1.0F) * 16.0F);
        double d1 = this.entity.getY() + (double) ((random.nextFloat() * 2.0F - 1.0F) * 16.0F);
        double d2 = this.entity.getZ() + (double) ((random.nextFloat() * 2.0F - 1.0F) * 16.0F);
        this.entity.getMoveControl().setWantedPosition(d0, d1, d2, 1.0D);
    }
}
